﻿using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;

namespace redis_microservice.Controllers
{
    /// <summary>
    /// 字符串 -> 常用于存储 JSON、简单数据, 统计数量等
    /// </summary>
    public class StringController(IConnectionMultiplexer connection) : BaseController(connection)
    {
        /// <summary>
        /// 获取值
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> Get(string key = "user:code:001")
        {
            string? val = await _db0.StringGetAsync(key);
            return Ok(val);
        }

        /// <summary>
        /// 设置值
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Set(string key = "user:code:001", string val = "666")
        {
            bool res = await _db0.StringSetAsync(key, val);
            return Ok(res);
        }

        /// <summary>
        /// 加一
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Increment(string key = "product:stock:001")
        { 
            long sum = await _db0.StringIncrementAsync(key);
            return Ok(sum);
        }

        /// <summary>
        /// 减一
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Decrement(string key = "product:stock:001")
        {
            long sum = await _db0.StringDecrementAsync(key);
            return Ok(sum);
        }
    }
}
